package filePlugin;

import java.io.IOException;
import java.io.Serializable;
import java.util.Vector;


public class Files implements Serializable {

  private static final long serialVersionUID = 1L;
  private Vector<OneFile> filelist;

  public Vector<OneFile> getFilelist() {
	return filelist;
}

public Files() {
    this.filelist = new Vector<OneFile>();
  }
  
  boolean compareChecksumWithFilelist(Object obj) {
    for(OneFile elem:this.filelist) {
      if(elem.equals((obj))) {
        return true;
      }
    }
    return false;
  }

  @Override
  public String toString() {
    int i = 0;
    String ret = new String("");
    if (this.filelist != null) {
      ret = "[";
      while (i < this.filelist.size()) {
        ret += this.filelist.elementAt(i).toString();
        i++;
      }
      ret += "]";
    }
    return ret;
  }

  public boolean add(Object obj) {
    if (this.compareChecksumWithFilelist(obj)) {
      System.out.println("[Files-ADD] ERROR: "+((OneFile) obj).getName()+" file already in db!");
      return false;
    } else {
      this.filelist.add((OneFile) obj);
      System.out.println("[Files-ADD] "+((OneFile) obj).getName()+" file added to db!");
      return true;
    }
  }

  public boolean delete(Object obj) {
    OneFile deleteMe = new OneFile((OneFile) obj);
    for(OneFile elem:this.filelist) {
      if(elem.getName().equalsIgnoreCase(deleteMe.getName())) {
        System.out.println("[Files-DELETE] Name of element matched with database - building temporary OneFile object for deletion");
        deleteMe = new OneFile(elem.getName(),elem.getBytes());
      }
    }
    if (this.compareChecksumWithFilelist(deleteMe)) {
      System.out.println("[Files-DELETE] "+ deleteMe.getName() + " found and deleted");
      this.filelist.remove(deleteMe);
      return true;
    } else {
      System.out.println("[Files-DELETE] "+ deleteMe.getName() + " not found in database - no deletion done");
      return false;
    }
  }
  
  //http://www.java-tips.org/java-se-tips/java.io/reading-a-file-into-a-byte-array.html
  @SuppressWarnings("resource")
  public static byte[] getBytesFromFile(java.io.File file) throws IOException {
    java.io.InputStream is = new java.io.FileInputStream(file);

    // Get the size of the file
    long length = file.length();

    if (length > Integer.MAX_VALUE) {
        // File is too large
    }

    // Create the byte array to hold the data
    byte[] bytes = new byte[(int)length];

    // Read in the bytes
    int offset = 0;
    int numRead = 0;
    while (offset < bytes.length
           && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
        offset += numRead;
    }

    // Ensure all the bytes have been read in
    if (offset < bytes.length) {
        throw new IOException("Could not completely read file "+file.getName());
    }

    // Close the input stream and return bytes
    is.close();
    return bytes;
  }
}
/* 

*/